var listView;
var type;
var schoolId;
var schName;
var rightObj;
var searchDom;
var listDom;
$(document).ready(function () {
    listView = $('#listView');
    schoolId = addressUri.getParam('schoolId');
        type = addressUri.getParam('type');
    schName =$('#targetPanel-title',window.parent.document).text().split('-')[0];
    loadData(schoolId,type);

    var str = '<div class="m-right">' +
                    '<a  href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:\'icon-search\',plain:true"></a>' +
              '</div>';
    rightObj = $(str).appendTo($('.m-toolbar',window.top.document));
    $.parser.parse(rightObj);
    $('.m-toolbar',window.top.document).on('click','.m-right a',function () {
        if(searchDom == null){
            searchDom =$('<li><input id="search_key" class="easyui-textbox" style="width:100%" data-options="buttonText:\'搜索&nbsp;&nbsp;\',buttonIcon:\'icon-search\'"></li>');
            $('#listView li').first().before(searchDom);
            $.parser.parse(searchDom);
        }else {
            searchDom.toggle();
        }
    });

    $('#listView').on('click','.textbox-button',function () {
       var key  = $('#search_key').textbox('getValue');
        addParams({schoolId:schoolId,type:type,key:key})
    });


    window.top.bindBack(function () {  //返回页面
        if(rightObj!=null){
            rightObj.remove();
            rightObj = null;
        }
        var text='';
        if(type == 1){ //学生、
            text += '学生-学校列表';
        }else {  //教师
            text += '教师-学校列表';
        }
        $('#targetFrame',window.parent.document).attr('src',baseUrl+'mobile/shortcut/schList?type='+type);
        window.parent.setTitle(text);
    });
});
function openit(target){ //跳转页面
    if(rightObj!=null){
        rightObj.remove();
        rightObj = null;
    }
    var id   = $(target).attr('nodeId');
    var stuName = $(target).text();
    if(type == 1){ //学生
        text = '学生-'+stuName;
    }else {  //教师
        text = '教师-'+stuName;
    }
    $('#targetFrame',window.parent.document).attr('src',baseUrl+'mobile/shortcut/userManage?userId='+id+'&type='+type+'&schoolId='+schoolId+'&schoolName='+escape(schName));
    window.parent.setTitle(text);
    window.parent.goTarget();
}

function loadData(schoolId,type) {
    $.ajax({
        url:baseUrl+'mobile/shortcut/getUserInfo',
        type:'POST',
        dataType:'json',
        data:{schoolId:schoolId,type:type},
        success:function (data) {
            var str = '';
            if(type == 4){
                $.each(data,function (id,item) {
                    str +='<li><a  nodeId="'+item['iUserId']+'" href="javascript:void(0)" onclick="openit(this)">'+item['sUserName']+'('+item['sLogonName']+')</a></li>';
                })
            }else {
                $.each(data,function (key,items) {
                    str +='<li class="m-list-group">'+key+'</li>';
                    $.each(items,function (index,value) {
                        str +='<li><a  nodeId="'+value['iUserId']+'" href="javascript:void(0)" onclick="openit(this)">'+value['sUserName']+'('+value['sLogonName']+')</a></li>';
                    });
                });
            }
            listDom = $(str);
            if(data.length!=0){
                $('.m-list').append(listDom);
            }

        },
        error:function (data) {
            console.log(data);
        }
    });
}

function addParams(params) {
    $.ajax({
        url:baseUrl+'mobile/shortcut/getUserInfo',
        type:'POST',
        dataType:'json',
        data:params,
        success:function (data) {
            var str = '';
            listDom.remove();
            if(params.type == 4){
                $.each(data,function (id,item) {
                    str +='<li><a  nodeId="'+item['iUserId']+'" href="javascript:void(0)" onclick="openit(this)">'+item['sUserName']+'('+item['sLogonName']+')</a></li>';
                })
            }else {
                $.each(data,function (key,items) {
                    str +='<li class="m-list-group">'+key+'</li>'
                    $.each(items,function (index,value) {
                        str +='<li><a  nodeId="'+value['iUserId']+'" href="javascript:void(0)" onclick="openit(this)">'+value['sUserName']+'('+value['sLogonName']+')</a></li>';
                    });
                });

            }
            listDom = $(str);
            if(data.length!=0){
                $('.m-list').append(listDom);
            }

        },
        error:function (data) {
            console.log(data);
        }
    });
}